package com.kehutong.basic.annex.mini;

import javax.servlet.http.HttpServletRequest;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.util.Objects;
import com.kehutong.basic.annex.entity.BasicAnnex;
import com.kehutong.basic.annex.service.BasicAnnexService;
import com.kehutong.common.TokenService;
import com.kehutong.common.errcode.Result;
import com.kehutong.common.util.Token;

/**
 * 公用附件
 */
@MvcAction
@ReqMapping("/kht-bin/mini/basic/annex")
public class BasicAnnexAction {

    @Inject
    private BasicAnnexService annexService;
    @Inject
    private TokenService tokenService;

    @ReqMapping("/list/page")
    public Object list(JSONObject jsonObject){
        return Result.success(annexService.listPage(jsonObject));
    }

    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(HttpServletRequest req, JSONObject jsonObject){
        Token token = null;
        try {
             token = tokenService.getUserToken(req);
            if (!checkLogin(token)) {
                return Result.error(0, "请先登录");
            }

            if (!checkLogin(token)) {
                return Result.error(0, "请先登录");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(0, "token接口异常");
        }
        return annexService.saveAnnex(token, jsonObject);
    }


    private boolean checkLogin(Token token){
        if (Objects.isNull(token)) {
            return false;
        }

        if (Objects.isEmpty(token.getUuid()) && Objects.isEmpty(token.getUsername())) {
            return false;
        } else {
            return true;
        }
    }

}
